package phase3;
import java.util.*;


public class FindByPath implements Algorithm {
	private Dictionary[] dictionaries;
	private int level;
	private int currentDictIndex;
	//recursive method
	//block:start point, str:the temp string result from parent blocks
	//strVector:the right words vector, wordLength:limited word length on searching
	public void findTheWords(Block block,Vector<String> strVector,int wordLength){
		int numOfChar=block.getNumOfChar();
		dictionaries=new Dictionary[numOfChar];
		for(int i=0;i<numOfChar;i++){
			dictionaries[i]=new Dictionary(block.getChar(i));
		}
		level=wordLength;
		currentDictIndex=-1;
		recursiveSearch(block,"",strVector,wordLength);
	}
	
	private void recursiveSearch(Block block,String str,Vector<String> strVector,int wordLength){
		//base case
		//if word length equals to 1, it just checks the current block
		if(wordLength==1){
			String temp;
			int blockCharCount=block.getNumOfChar();
			for(int i=0;i<blockCharCount;i++){
				temp=str+block.getChar(i);
				if(dictionaries[currentDictIndex].check(temp)){
					if(!strVector.contains(temp))
						strVector.add(temp);
				}
			}	
		}else{
			//else append the char at current block and check if it is valid
			//pass to next block with that string
			String temp;
			int blockCharCount=block.getNumOfChar();
			Vector<Block> nextBlocks=block.getNextBlock();
			int nextBlocksSize=nextBlocks.size();
			for(int i=0;i<blockCharCount;i++){
				if(level==wordLength)
					currentDictIndex++;
				temp=str+block.getChar(i);
				if(dictionaries[currentDictIndex].check(temp)){
					if(!strVector.contains(temp))
						strVector.add(temp);
				}
				for(int j=0;j<nextBlocksSize;j++){
					recursiveSearch(nextBlocks.get(j),temp,strVector,wordLength-1);
				}
			}
		}
	}
	
//	private Dictionary getDictionary(String s){
//		if(s==""){
//			return dictionaries[0];
//		}
//		char firstChar=s.charAt(0);
//		
//		return dictionaries[0];
//	}
}
